import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
/**
 * Generated class for the AssistantfixedlistPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-assistantfixedlist',
  templateUrl: 'assistantfixedlist.html',
})
export class AssistantfixedlistPage {


	private listItemsPage: any;

	/**
	 * 列表
	 */
	private listItems: Array<{
		id: any,
		assetCode: any,
		assetName: any,
		specFormula: any,
		numbers: any,
		originalValue: any,
		manageDept: any,
		assetBelong: any,
		updateDate: any
	}>;

	/**
	 * 选择框
	 */
	private inputSearch = {
		title: '',
		no: '',
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider) {
		this.listItems = new Array();
		this.listItemsPage = 1;
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad AssistantfixedlistPage');
		this.loadData();
	}

	loadData() {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				//初始化
				this.listItemsPage = 1;
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					pageNo: this.listItemsPage,
					code: this.inputSearch.no,
					name: this.inputSearch.title
				};
				//收文传阅列表
				this.http.postJson('oa', 'assets/list.do', params).then(res => {
					if (res['data'] != undefined) {
						let datas = res['data']['name'];
						this.loadMakeData(true, datas);
						if (success)
							success();
					} else {
						if (success)
							success();
					}
				});
			});
		});
	}
	//加载更多
	loadMoreData() {
		//异步方式
		return new Promise((success) => {
			this.storage.getStorage("user").then(val => {
				//请求方式如下
				let params = {
					user_id: val['user_id'],
					pageNo: this.listItemsPage + 1,
					code: this.inputSearch.no,
					name: this.inputSearch.title
				};
				//收文传阅列表
				this.http.postJson('oa', 'assets/list.do', params).then(res => {
					if (res['data'] != undefined) {
						let datas = res['data']['name'];
						this.loadMakeData(false, datas);
						if (datas.length > 0)
							this.listItemsPage++;
						if (success)
							success();
					} else {
						if (success)
							success();
					}
				});
			});
		});
	}
	//共通列表
	loadMakeData(isnew: any, datas: any) {
		if (isnew)
			this.listItems = new Array();
		for (let i = 0; i < datas.length; i++) {
			let item = datas[i];
			this.listItems.push({
				id: item['id'],
				assetCode: item['assetCode'],
				assetName: item['assetName'],
				specFormula: item['specFormula'],
				numbers: item['numbers'],
				originalValue: item['originalValue'],
				manageDept: item['manageDept'],
				assetBelong: item['assetBelong'],
				updateDate: item['updateDate'],
			});
		}
	}
	//搜索
	dosearch() {
		let data = new Object();
		let params = new Object();
		params['applyapplytitle'] = this.inputSearch.title;
		params['applyapplyno'] = this.inputSearch.no;
		data['type'] = 5; //检索类型
		data['params'] = params; //参数传递 
		let opts = new Object();
		this.dialog.showModal("ModalsearchPage", data, opts).then(success => {
			//界面关闭的时候
			if (success) {
				this.inputSearch.title = success['applyapplytitle'];
				this.inputSearch.no = success['applyapplyno'];
				this.loadData();
			}
		});
	}

	/*下拉加载*/
	doRefresh(refresher) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			/*判断是哪个列表显示*/
			this.loadData().then(success => {
				refresher.complete();
			});
		}, 1500);
	}
	/*上拉加载*/
	doInfinite(infiniteScroll) {
		/*判断是哪个列表显示*/
		setTimeout(() => {
			this.loadMoreData().then(success => {
				infiniteScroll.complete();
			});
		}, 1500);
	}
	//详情
	dodetail(item: any) {
		this.navCtrl.push("AssistantfixeddetailPage", {
			type: 1
		});
	}
	//修改
	doedit(item: any) {
		this.navCtrl.push("AssistantfixededitPage", {
			type: 1
		});
	}
	//删除
	dodel(item: any) {
		this.dialog.showConfirm("确认", "确认要删除该固定资产库存管理吗？").then(success => {

		}, fail => {

		});
	}
}
